//
// Created by yanhai on 2020/3/31.
//

#include <string>
#include "server/http_server.hpp"

/**
 * test:
 *  curl -X POST 'http://localhost:8000/v1/test' \
--header 'Content-Type: application/json' \
--data-raw '{"aa": "中文测试", "bb":"bb", "cc": ["cc1", "cc2"], "ee":"ee"}'
 * @param request
 * @param response
 * @param arg
 */
static void test(const Request *request, Response *response)
{
    std::string res;
    auto &data = request->json();
    auto aa = data["aa"].asString();
    auto bb = data["bb"].asString();
    auto cc = data["cc"];
    auto ee = data["ee"].asString();

    res = "aa:";
    res += aa;
    res += "; bb:";
    res += bb;
    res += "; cc:";
    for (auto &item: cc) {
        res += item.asString() + ",";
    }
    res += "; ee:";
    res += ee;

    response->write(res);
}

int main()
{
    auto &httpServer = HTTPServer::instance();
    httpServer.set_cb("/v1/test", test, EVHTTP_REQ_POST);
    httpServer.listen(8000, "0.0.0.0");
    httpServer.start();
    return 0;
}
